﻿using System;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace TestComparer
{
    class TextblockWriter
    {
        private readonly TextBlock textBlock;

        public Color Foreground { get; set; }
        public Color Background { get; set; }

        public TextblockWriter(TextBlock textBlock)
        {
            this.textBlock = textBlock;
            this.Foreground = Colors.Black;
            this.Background = Colors.Transparent;
        }

        public void WriteLine(object text, params object[] parameters)
        {
            this.WriteFormat(text, parameters);
            this.WriteLine();
        }

        public void WriteLine()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine();
            this.Write(builder.ToString());
        }

        public void WriteFormat(object text, params object[] parameters)
        {
            if (parameters == null || parameters.Length == 0)
            {
                this.Write(text);
            }
            else
            {
                this.Write(string.Format(text.ToString(), parameters));
            }
        }

        public void Clear()
        {
            this.textBlock.Text = string.Empty;
        }

        private void Write(object text)
        {
            Run run = new Run(text.ToString());
            run.Foreground = new SolidColorBrush(this.Foreground);
            run.Background = new SolidColorBrush(this.Background);

            this.textBlock.Inlines.Add(run);
        }
    }
}
